<?php


namespace console\spider;


use backend\models\School;
use backend\models\Student;
use common\models\Person;
use console\spider\utils\FileUtils;
use console\spider\utils\HttpClient;
use console\spider\utils\TimeUtils;

class StudentSpider
{

    /**
     * 获取学生数据并保存在data目录
     */
    public static function getStudents()
    {
        $params = Array();
        $params['dir'] = Params::studentDir;
        $url = Params::studentUrl;
        $params['fileName'] = "student";
        $params['tag']= "学生信息";
        HttpClient::dataSave($url,$params);
    }

    public static function analysisStudentsData()
    {
        $files = FileUtils::findFileName(Params::studentDir);
        foreach ($files as  $fileName) {
            $studentsJson = json_decode(file_get_contents(Params::studentDir . $fileName));
            $studentList = $studentsJson->resultList;
            foreach ($studentList as $student) {

                $person = Person::findOne([
                    "id_number" => $student->entity->sfzjh
                ]);

                if (empty($person)) {
                    $person = new Person();
                    $person->create_time = date('Y-m-d H:i:s');
                }
                $person->name = $student->entity->xm;
                $person->sex = $student->entity->xbm;
                $person->id_number = $student->entity->sfzjh;
                $person->birthday = $student->entity->csrq;
                $person->nationality = '中国';
                $person->id_type = "居民身份证";
                $person->ethnicity = $student->idMap->mzm;
                $person->native_place = $student->entity->jg;
                if ($person->save())
                    echo '.';
                else
                    var_dump($person);


                $model = Student::findOne([
                    "GRID" => $person->id
                ]);

                if (empty($model)) {
                    $model = new Student();
                    $model->GRID = $person->id;
                }

                $school = School::findOne([
                    'ZWMC' => $student->entity->xxmc
                ]);

                $model->XXID = $school->ID;//学校ID
                $model->BJID = "";//班级ID
                $model->XH = $student->entity->xh;//学号
                $model->XSXM = $student->entity->xm;
                $model->XB = $student->entity->xbm;
                $model->SFZHM = $student->entity->sfzjh;
                $model->RXSJ = $student->entity->rxny . '01';
                $model->LXSJ = "";//离校时间
                $model->ZT = "在读";

                if ($model->save())
                    echo '.';
                else
                    var_dump($model);
            }
        }
    }

    
}